package com.qiaofang.controller;

import com.qiaofang.common.response.CommonResponse;
import com.qiaofang.common.response.SuccessfulResponseFactory;
import com.qiaofang.constant.CompanyTypeEnum;
import com.qiaofang.dto.DistributionCompanyDTO;
import com.qiaofang.dto.ListDistributionCompanyDTO;
import com.qiaofang.request.*;
import com.qiaofang.response.GetDistributionCompanyResponse;
import com.qiaofang.response.ListDistributionCompanyResponse;
import com.qiaofang.response.ListOptionCompanyResponse;
import com.qiaofang.service.DistributionCompanyService;
import com.qiaofang.service.PartnerInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.MimeTypeUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@RestController
@Api(tags = {"分销机构接口"})
@RequestMapping(value = "/distributionCompany", produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
public class DistributionCompanyController {

    @Autowired
    private DistributionCompanyService distributionCompanyService;

    @Autowired
    private PartnerInfoService partnerInfoService;

    @PostMapping(value = "/listCompany")
    @ApiOperation(value = "查询列表")
    public ListDistributionCompanyResponse listCompany(@RequestBody ListDistributionCompanyRequest request) {
        ListDistributionCompanyResponse response = new ListDistributionCompanyResponse();
        ListDistributionCompanyDTO dto = new ListDistributionCompanyDTO();
        dto.setStatus(request.getStatus());
        dto.setType(request.getType());
        dto.setMerchantCompanyId(request.getMerchantCompanyId());
        dto.setProvince(request.getProvince());
        dto.setCity(request.getCity());
        dto.setZone(request.getZone());
        dto.setCreateTimeStart(request.getCreateTimeStart());
        dto.setCreateTimeEnd(request.getCreateTimeEnd());
        dto.setSearchContent(request.getSearchContent());
        response.setData(distributionCompanyService.listDistributionCompany(dto, request.getPage()));
        return response;
    }

    @PostMapping(value = "/listOptionCompany")
    @ApiOperation(value = "公司列表(下拉框)")
    public ListOptionCompanyResponse listOptionCompany() {
        ListOptionCompanyResponse response = new ListOptionCompanyResponse();
        response.setData(distributionCompanyService.listOptionCompany(CompanyTypeEnum.Intermediary.getType()));
        return response;
    }

    @PostMapping(value = "/listMerchantPartnerInfo")
    @ApiOperation(value = "渠道经理列表")
    public ListOptionCompanyResponse listMerchantPartnerInfo(@RequestBody @Valid ListMerchantPartnerInfoRequest request) {
        ListOptionCompanyResponse response = new ListOptionCompanyResponse();
        response.setData(partnerInfoService.listPartnerInfo(CompanyTypeEnum.Developer.getType(), request.getCompanyId()));
        return response;
    }

    @PostMapping(value = "/addCompany")
    @ApiOperation(value = "新增")
    public CommonResponse addCompany(@RequestBody @Valid AddDistributionCompanyRequest request) {
        DistributionCompanyDTO dto = new DistributionCompanyDTO();
        dto.setCompanyName(request.getCompanyName());
        dto.setStatus(request.getStatus());
        dto.setCompanyType(request.getCompanyType());
        dto.setMerchantCompanyId(request.getMerchantCompanyId());
        dto.setProvince(request.getProvince());
        dto.setCity(request.getCity());
        dto.setZone(request.getZone());
        dto.setAddress(request.getAddress());
        dto.setPartnerInfoId(request.getPartnerInfoId());
        distributionCompanyService.addDistributionCompany(dto);
        return SuccessfulResponseFactory.getSuccessResponse();
    }

    @PostMapping(value = "/updateCompany")
    @ApiOperation(value = "编辑")
    public CommonResponse updateCompany(@RequestBody @Valid UpdateDistributionCompanyRequest request) {
        DistributionCompanyDTO dto = new DistributionCompanyDTO();
        dto.setId(request.getId());
        dto.setCompanyName(request.getCompanyName());
        dto.setStatus(request.getStatus());
        dto.setCompanyType(request.getCompanyType());
        dto.setMerchantCompanyId(request.getMerchantCompanyId());
        dto.setProvince(request.getProvince());
        dto.setCity(request.getCity());
        dto.setZone(request.getZone());
        dto.setAddress(request.getAddress());
        dto.setPartnerInfoId(request.getPartnerInfoId());
        distributionCompanyService.updateDistributionCompany(dto);
        return SuccessfulResponseFactory.getSuccessResponse();
    }

    @PostMapping(value = "/getCompany")
    @ApiOperation(value = "查询详情")
    public GetDistributionCompanyResponse getCompany(@RequestBody @Valid GetCompanyRequest request) {
        GetDistributionCompanyResponse response = new GetDistributionCompanyResponse();
        response.setData(distributionCompanyService.getDistributionCompany(request.getId()));
        return response;
    }

    @PostMapping(value = "/enableCompany")
    @ApiOperation(value = "启用")
    public CommonResponse enableCompany(@RequestBody @Valid EnableCompanyRequest request) {
        distributionCompanyService.enableCompany(request.getId());
        return SuccessfulResponseFactory.getSuccessResponse();
    }

    @PostMapping(value = "/disableCompany")
    @ApiOperation(value = "禁用")
    public CommonResponse disableCompany(@RequestBody @Valid DisableCompanyRequest request) {
        distributionCompanyService.disableCompany(request.getId());
        return SuccessfulResponseFactory.getSuccessResponse();
    }

    @PostMapping(value = "/deleteCompany")
    @ApiOperation(value = "删除")
    public CommonResponse deleteCompany(@RequestBody @Valid DeleteCompanyRequest request) {
        distributionCompanyService.deleteCompany(request.getId());
        return SuccessfulResponseFactory.getSuccessResponse();
    }

    @PostMapping(value = "/batchEnableCompany")
    @ApiOperation(value = "批量启用")
    public CommonResponse batchEnableCompany(@RequestBody @Valid BatchEnableCompanyRequest request) {
        distributionCompanyService.batchEnableCompany(request.getIds());
        return SuccessfulResponseFactory.getSuccessResponse();
    }

    @PostMapping(value = "/batchDisableCompany")
    @ApiOperation(value = "批量禁用")
    public CommonResponse batchDisableCompany(@RequestBody @Valid BatchDisableCompanyRequest request) {
        distributionCompanyService.batchDisableCompany(request.getIds());
        return SuccessfulResponseFactory.getSuccessResponse();
    }

    @PostMapping(value = "/batchDeleteCompany")
    @ApiOperation(value = "批量删除")
    public CommonResponse batchDeleteCompany(@RequestBody @Valid BatchDeleteCompanyRequest request) {
        distributionCompanyService.batchDeleteCompany(request.getIds());
        return SuccessfulResponseFactory.getSuccessResponse();
    }

    @PostMapping(value = "/listOptionDistributionCompany")
    @ApiOperation(value = "根据渠道ID查询分销机构公司列表(下拉框)")
    public ListOptionCompanyResponse listOptionDistributionCompany(@RequestBody @Valid GetCompanyRequest request) {
        ListOptionCompanyResponse response = new ListOptionCompanyResponse();
        response.setData(distributionCompanyService.listOptionDistributionCompany(request.getId()));
        return response;
    }

}
